package com.spzx.channel.service.impl;

import com.alibaba.fastjson2.JSON;
import com.spzx.channel.configure.ThreadPoolConfig;
import com.spzx.channel.domain.ItemVo;
import com.spzx.channel.service.IItemService;
import com.spzx.common.core.constant.SecurityConstants;
import com.spzx.common.core.domain.R;
import com.spzx.common.core.exception.ServiceException;
import com.spzx.product.api.RemoteProductService;
import com.spzx.product.api.domain.Product;
import com.spzx.product.api.domain.ProductDetails;
import com.spzx.product.api.domain.ProductSku;
import com.spzx.product.api.domain.vo.SkuPrice;
import com.spzx.product.api.domain.vo.SkuStockVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ThreadPoolExecutor;

@Service
@Slf4j
public class ItemServiceImpl implements IItemService {

    @Autowired(required = false)
    private RemoteProductService remoteProductService;

    @Autowired
    RedisTemplate redisTemplate;

    @Autowired
    ThreadPoolExecutor threadPoolExecutor;

    @Override
    public ItemVo item(Long skuId) throws Exception {

        //用bitMap判断缓存中是否存有数据，如果存有则通过远程调取，没有则拒绝远程调用。
        String key = "sku:product:data";
        Boolean isExist = redisTemplate.opsForValue().getBit(key, skuId);
        if (!isExist) {
            log.error("用户查询商品不存在：{}", skuId);
            //查询数据不存在直接返回空对象
            throw new ServiceException("用户查询商品不存在");
        }

        ItemVo itemVo = new ItemVo();

        //任务1.获取sku信息
//        R<ProductSku> productSkuResult = remoteProductService.getProductSku(skuId, SecurityConstants.INNER);
//        if (R.FAIL == productSkuResult.getCode()) {
//            throw new ServiceException(productSkuResult.getMsg());
//        }
//        ProductSku productSku = productSkuResult.getData();
//        itemVo.setProductSku(productSku);
        CompletableFuture<ProductSku> productSkuCompletableFuture = CompletableFuture.supplyAsync(() -> {
            R<ProductSku> productSkuResult = remoteProductService.getProductSku(skuId, SecurityConstants.INNER);
            if (R.FAIL == productSkuResult.getCode()) {
                throw new ServiceException(productSkuResult.getMsg());
            }
            ProductSku productSku = productSkuResult.getData();
            itemVo.setProductSku(productSku);
            return productSku;
        }, threadPoolExecutor);


        //任务2.获取商品信息
//        R<Product> productResult = remoteProductService.getProduct(productSku.getProductId(), SecurityConstants.INNER);
//        if (R.FAIL == productResult.getCode()) {
//            throw new ServiceException(productResult.getMsg());
//        }
//        Product product = productResult.getData();
//        itemVo.setProduct(product);
//        itemVo.setSliderUrlList(Arrays.asList(product.getSliderUrls().split(",")));
//        itemVo.setSpecValueList(JSON.parseArray(product.getSpecValue()));
        CompletableFuture<Void> productCompletableFuture = productSkuCompletableFuture.thenAcceptAsync((productSku) -> {
            R<Product> productResult = remoteProductService.getProduct(productSku.getProductId(), SecurityConstants.INNER);
            if (R.FAIL == productResult.getCode()) {
                throw new ServiceException(productResult.getMsg());
            }
            Product product = productResult.getData();
            itemVo.setProduct(product);
            itemVo.setSliderUrlList(Arrays.asList(product.getSliderUrls().split(",")));
            itemVo.setSpecValueList(JSON.parseArray(product.getSpecValue()));
        }, threadPoolExecutor);


        //任务3.获取商品最新价格
//        R<SkuPrice> skuPriceResult = remoteProductService.getSkuPrice(skuId, SecurityConstants.INNER);
//        if (R.FAIL == skuPriceResult.getCode()) {
//            throw new ServiceException(skuPriceResult.getMsg());
//        }
//        SkuPrice skuPrice = skuPriceResult.getData();
//        itemVo.setSkuPrice(skuPrice);
        CompletableFuture<Void> skuPriceCompletableFuture = CompletableFuture.runAsync(() -> {
            R<SkuPrice> skuPriceResult = remoteProductService.getSkuPrice(skuId, SecurityConstants.INNER);
            if (R.FAIL == skuPriceResult.getCode()) {
                throw new ServiceException(skuPriceResult.getMsg());
            }
            SkuPrice skuPrice = skuPriceResult.getData();
            itemVo.setSkuPrice(skuPrice);
        }, threadPoolExecutor);


        //任务4.获取商品详情
//        R<ProductDetails> productDetailsResult = remoteProductService.getProductDetails(productSku.getProductId(), SecurityConstants.INNER);
//        if (R.FAIL == productDetailsResult.getCode()) {
//            throw new ServiceException(productDetailsResult.getMsg());
//        }
//        ProductDetails productDetails = productDetailsResult.getData();
//        itemVo.setDetailsImageUrlList(Arrays.asList(productDetails.getImageUrls().split(",")));
        CompletableFuture<Void> productDetailsCompletableFuture = productSkuCompletableFuture.thenAcceptAsync((productSku) -> {
            R<ProductDetails> productDetailsResult = remoteProductService.getProductDetails(productSku.getProductId(), SecurityConstants.INNER);
            if (R.FAIL == productDetailsResult.getCode()) {
                throw new ServiceException(productDetailsResult.getMsg());
            }
            ProductDetails productDetails = productDetailsResult.getData();
            itemVo.setDetailsImageUrlList(Arrays.asList(productDetails.getImageUrls().split(",")));
        }, threadPoolExecutor);


        //任务5.获取商品规格对应商品skuId信息
//        R<Map<String, Long>> skuSpecValueResult = remoteProductService.getSkuSpecValue(productSku.getProductId(), SecurityConstants.INNER);
//        if (R.FAIL == skuSpecValueResult.getCode()) {
//            throw new ServiceException(skuSpecValueResult.getMsg());
//        }
//        Map<String, Long> skuSpecValueMap = skuSpecValueResult.getData();
//        itemVo.setSkuSpecValueMap(skuSpecValueMap);
        CompletableFuture<Void> skuSpecValueMapCompletableFuture = productSkuCompletableFuture.thenAcceptAsync((productSku) -> {
            R<Map<String, Long>> skuSpecValueResult = remoteProductService.getSkuSpecValue(productSku.getProductId(), SecurityConstants.INNER);
            if (R.FAIL == skuSpecValueResult.getCode()) {
                throw new ServiceException(skuSpecValueResult.getMsg());
            }
            Map<String, Long> skuSpecValueMap = skuSpecValueResult.getData();
            itemVo.setSkuSpecValueMap(skuSpecValueMap);
        }, threadPoolExecutor);


        //任务6.获取商品库存信息
//        R<SkuStockVo> skuStockResult = remoteProductService.getSkuStock(skuId, SecurityConstants.INNER);
//        if (R.FAIL == skuStockResult.getCode()) {
//            throw new ServiceException(skuStockResult.getMsg());
//        }
//        SkuStockVo skuStockVo = skuStockResult.getData();
//        itemVo.setSkuStockVo(skuStockVo);
//        productSku.setStockNum(skuStockVo.getAvailableNum());
        CompletableFuture<Void> skuStockVoCompletableFuture = productSkuCompletableFuture.thenAcceptAsync((productSku) -> {
            R<SkuStockVo> skuStockResult = remoteProductService.getSkuStock(skuId, SecurityConstants.INNER);
            if (R.FAIL == skuStockResult.getCode()) {
                throw new ServiceException(skuStockResult.getMsg());
            }
            SkuStockVo skuStockVo = skuStockResult.getData();
            itemVo.setSkuStockVo(skuStockVo);
            productSku.setStockNum(skuStockVo.getAvailableNum());
        }, threadPoolExecutor);


        //组合六个子任务，返回itemVo
        CompletableFuture.allOf(productSkuCompletableFuture,
                productCompletableFuture,
                skuPriceCompletableFuture,
                productDetailsCompletableFuture,
                skuSpecValueMapCompletableFuture,
                skuStockVoCompletableFuture).join();


        return itemVo;
    }
}